package 力扣日常刷题.木23一月.第04天304;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 帅小伙
 * @date 2022/3/4
 * @description
 */
public class Demo43选择物品 {


    static ArrayList<ArrayList<Integer>> list = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(),m = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = i + 1;
        }

        boolean isUsed[] = new boolean[n];
        dfs(arr,isUsed,new ArrayList<>(),m);
        for (ArrayList<Integer> arrayList : list) {
            for (Integer integer : arrayList) {
                System.out.print(integer+" ");
            }
            System.out.println();
        }

    }


    public static void dfs(int[] arr,boolean[] isUsed,ArrayList<Integer> path,int m) {
        if(path.size() == m){
            list.add(new ArrayList<Integer>(path));
            return;
        }

        for (int i = 0; i < arr.length; i++) {
            if(!isUsed[i]){
                isUsed[i] = true;
                path.add(arr[i]);
                dfs(arr,isUsed,path,m);
                path.remove(path.size() - 1);
                isUsed[i] = false;
            }
        }
    }

}
